<%-- 
    Document   : lienheCanhan
    Created on : Dec 18, 2013, 6:20:33 AM
    Author     : Huyen
--%>

<%@page import="bc.entity.Binhluan"%>
<%@page import="businesscard.DAO.BinhluanDAO"%>
<%@page import="bc.entity.Congty"%>
<%@page import="java.util.Date"%>
<%@page import="businesscard.DAO.UserDAO"%>
<%@page import="bc.entity.User"%>
<%@page import="bc.entity.Lienhe"%>
<%@page import="businesscard.DAO.LienheDAO"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<script>
    var xmlhttp;
    var x;
    function comment(id, y)
    {
        xmlhttp = GetXmlHttpObject();

        if (xmlhttp == null)
        {
            alert("Your browser does not support Ajax HTTP");
            return;
        }
        var cmt;
        cmt = document.getElementById("binhluan" + y).value;
        var url = "comment.jsp";
        url = url + "?id=" + id + "&cmt=" + cmt;

        xmlhttp.onreadystatechange = getOutput;
        xmlhttp.open("GET", url, true);
        xmlhttp.send(null);
        x = y;
    }

    function getOutput()
    {
        if (xmlhttp.readyState == 4 || xmlHttp.readyState == "complete")
        {
            document.getElementById("cmtNext" + x).innerHTML = xmlhttp.responseText;
        }
    }


    function phantrang(k) {
        xmlhttp = GetXmlHttpObject();

        if (xmlhttp == null)
        {
            alert("Your browser does not support Ajax HTTP");
            return;
        }
        //var page=document.getElementById("pageNumber").value;
        var url = "phantrang.jsp";
        url = url + "?page=" + k;
        xmlhttp.onreadystatechange = getOutPT;
        xmlhttp.open("GET", url, true);
        xmlhttp.send(null);
    }
    function getOutPT()
    {
        if (xmlhttp.readyState == 4 || xmlHttp.readyState == "complete")
        {
            document.getElementById("lienhemoichiase").innerHTML = xmlhttp.responseText;
        }
    }
    function GetXmlHttpObject()
    {
        if (window.XMLHttpRequest)
        {
            return new XMLHttpRequest();
        }
        if (window.ActiveXObject)
        {
            return new ActiveXObject("Microsoft.XMLHTTP");
        }
        return null;
    }
</script>
<%
    User user3 = (User) request.getSession().getAttribute("USER");
    String username1 = user3.getUser();
    UserDAO userDAO1 = new UserDAO();
    User user4 = userDAO1.getUser2(username1);
    int id_user = user4.getIdUser();
    LienheDAO lhDAO = new LienheDAO();
%>

<%
    int perpage = 2;
    String p = request.getParameter("page");
    int get_trang;
    if(p==null) get_trang=1;
    else get_trang=Integer.parseInt(p);
    int sotrang;
    int countLH = lhDAO.countLH(id_user);
    if (countLH % perpage != 0) {
        sotrang = countLH / perpage + 1;
    } else {
        sotrang = countLH / perpage;
    }       
    Lienhe[] lh = lhDAO.getLienHeTheoTrang(id_user, 0, perpage);

%>

<div class="lienhemoichiase" id="lienhemoichiase">

    <%


        for (int i = 0; i < lh.length; i++) {

            int id_lienhe = lh[i].getIdLienhe();
            int id_congty = lh[i].getCongty().getIdCongty();
            String congty = lhDAO.getTenCongty(id_congty);
            String ten = lh[i].getTen();
            String tell = lh[i].getSdtcodinh();
            String mobi = lh[i].getSdt();
            String fax = lh[i].getFax();
            String email1 = lh[i].getEmail();
            String diachi = lh[i].getDiachi();
            String huyen = lh[i].getQuanhuyen().getQuanhuyen();
            String tinh = lh[i].getQuanhuyen().getThanhpho().getThanhpho();
            String capbac = lh[i].getCapbac().getCapbac();
            String bophan = lh[i].getBophan().getBophan();
    %>
    <div class="lienhe">
        <div class="like"><a href="#">Like</a></div>
        <div class="tencty"><%=congty%></div>
        <div class="tenlh"><%=ten%></div>
        <div class="capbac"><%=capbac%> - bộ phận <%=bophan%></div>
        <div class="tell">Tell:<%=tell%> </div>
        <div class="tell">Mobile:<%=mobi%></div>
        <div class="tell">Fax:<%=fax%></div>
        <div class="tell">Email:<%=email1%></div>
        <div class="add">Add:<%=diachi + ", huyện " + huyen + ", tỉnh " + tinh%></div>
    </div>
    <div class="comment" id="cmtNext<%=i%>">
        <%
            BinhluanDAO A = new BinhluanDAO();
            Binhluan[] B = A.getAllBinhluan(id_lienhe);
            if (B != null) {
                for (int j = 0; j < B.length; j++) {
        %>

        <div class="hinhdaidien">
            <%
                int id = B[j].getUser().getIdUser();
                String link1 = "avatar/" + A.getLinkHinh(id);
            %>
            <image src="<%=link1%>" style="width: 40px; height: 40px;"/>
        </div>
        <div id="usercmt"class="usercmt"><%=B[j].getBinhluan()%></div>
        <%                    }
            }

        %>
    </div>
    
    <div class="binhluan">

        <input type="text" name="binhluan" value=" " placeholder="Viết bình luận..." id="binhluan<%=i%>" />
        <input type="hidden" name="binhluanhide" value="<%=id_lienhe%>" id="txtID"/>
        <input type="submit" value="Bình luận" name="action" onclick="comment('<%=id_lienhe%>', '<%=i%>');"/>   

    </div>
    <%
        }
    %>

    <div class="phantrang" style="margin-left:280px;">
    <% 
        String abc=lhDAO.phantrang(sotrang, get_trang); 
        out.print(abc);
    %>
    </div>
</div>

